package com.fyzk.activity.chongzhi;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppProgressDialog;
import com.fyzk.activity.app.AppToast;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.dialog.DJ_ProgressDialog;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.HandlerTypeUtils;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;

import http.Common.CommandUtil;
import http.StringResultBean;

public abstract class CZ_BaseActivity extends BaseActivity{
	
	protected TopTitleView zfb_title_rl;
	private EditText money;
	private TextView sure;
	protected LinearLayout mL_main;
	
	
	protected  String mInt_rechargeType;//充值类型
	
	protected String amount;
	

//	protected TextView topTitle;
	protected TextView tishi1;
	protected TextView tishi2;
	
	public static final int RECHARGE_EXPRESS = 0;
	public static final int RECHARGE_WAP = 1;
	public static final int RECHARE_YL = 3;//银联支付
	public static final String RECHARGE_JD = "jdwappay";//京东支付

	private int minMoney = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInt_rechargeType = initRechargeType();
		setContentView(R.layout.cz_zhifubao);
		info();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	public abstract String initRechargeType();
	
	private void info() {
		mL_main = (LinearLayout) findViewById(R.id.dj_alipay_main);
		zfb_title_rl = (TopTitleView)findViewById(R.id.topview);
		money = (EditText) findViewById(R.id.zfb_money);
		money.setText(""+minMoney);
		sure = (TextView) findViewById(R.id.zfb_sure);

		sure.setOnClickListener(this);
		zfb_title_rl.setOnTitleClickListener(this);

//		topTitle = (TextView)findViewById(R.id.zfb_top_tv);
		tishi1 = (TextView)findViewById(R.id.zf_tishi_amount);
		tishi2 = (TextView)findViewById(R.id.zf_tishi2);
		initViewData();
		
	}

	@Override
	protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
		super.onSuccess(requestCode, resultBean, result);
	}

	public void initViewData(){
		
	}
	
	protected void recharge(String money){
		this.amount = money;
//		request(SchemaDef.RECHARGE_ALIPAY_EXPRESS);
//		showNetProgress();

		try {
			JSONObject params = new JSONObject();
			params.put("amount",money);
			params.put("authtoken", MySharedPreferences.getInstance().getAuthtoken());
			params.put("payChannel",initRechargeType());
			httpPostJson(CommandUtil.RECHARGE, Commands.USERPAY, params, false);
		} catch (JSONException e) {
			e.printStackTrace();
		}


	}
	





	
//	@Override
//	public void onProtocalSuccess(Object obj) {
//		super.onProtocalSuccess(obj);
//		cancleNetProgress();
//		mObj_response = (RechargeAlipayExpressResponseData) obj;
//		CommonResult result = mObj_response.commonResult;
//		Message msg = mHandler.obtainMessage();
//		msg.obj = result.tips;
//		if (result.codeValue == 0) {
//			msg.what = HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA;
//		} else if (result.codeValue == HandlerTypeUtils.ZY_HANDLER_TYPE_LOGOUT) {
//			msg.what = HandlerTypeUtils.ZY_HANDLER_TYPE_LOGOUT;
//		} else {
//			msg.what = HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA_FAIL;
//		}
//		mHandler.sendMessage(msg);
//	}
//
	AppProgressDialog progressDialog = null;
	
	public void showNetProgress(){
		if(null == progressDialog){
			progressDialog = new AppProgressDialog(this);
		}
		progressDialog.show();
	}
	
	public void  cancleNetProgress(){
		if(null != progressDialog){
			progressDialog.cancel();
		}
	}
	
	/**
	 * 充值方法
	 */
	public abstract void recharge();
	
	@Override
	protected void parserMessage(Message msg) {
		super.parserMessage(msg);
		DJ_ProgressDialog.dissmiss();
		switch (msg.what) {
		case HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA:
			recharge();
			break;
		case HandlerTypeUtils.ZY_HANDLER_TYPE_LOAD_DATA_FAIL:
			AppToast.getToast().show(String.valueOf(msg.obj));
			break;
		case HandlerTypeUtils.ZY_HANDLER_TYPE_LOGOUT:
			AppToast.getToast().show(String.valueOf(msg.obj));
			finish();
			break;
		}
	}
	
	@Override
	public void onClick(View arg0) {
		super.onClick(arg0);
		switch (arg0.getId()) {
		case R.id.zfb_sure:
			if(et_yanZheng()){
				recharge(money.getText().toString());
			}
			break;
		default:
			break;
		}
	}
	
	/**
	 * 输入框验证
	 * @return
	 */
	private boolean et_yanZheng() {
		if (isNull(money)) {
			AppToast.getToast().show("充值彩金不能为空！");
			return false;
		}
		long m = 0;
		try {
			m = Long.parseLong(money.getText().toString());
		} catch (Exception e) {
		}
		if (m < minMoney) {
			AppToast.getToast().show("充值彩金至少"+minMoney+"米！");
			return false;
		}
		if(m > 2000){
			money.setText("2000");
			AppToast.getToast().show("单笔最高充值为2000米！");
			return false;
		}

		return true;
	}
	
	/**
	 * 非空验证
	 */
	private boolean isNull(EditText editText) {
		if (editText.getText() == null
				|| editText.getText().toString().equals("")) {
			editText.requestFocus();
			return true;
		}
		return false;
	}


}
